package dateStructure;

import java.util.Scanner;

public class sequenceQueue {
    //顺序队列的实现
    private static final int Maxsize=107;
    private static class MQueue{
        int base[];
        int front;//定义一个头结点
        int rear;//定义一个尾结点
    }
    private static boolean initQueue(MQueue Q){
        Q.base=new int[Maxsize];
        if(Q.base==null){
            return false;
        }
        Q.front=Q.rear=0;
        return true;
    }
    private static boolean inQueue(MQueue Q,int e){
        if((Q.rear+1)%Maxsize==Q.front){///虽然还没有完全充满全部容量，留一个容量是为了和空队列的判断条件区分开来
            System.out.println("队列已满！");
            return false;
        }
        Q.base[Q.rear++]=e;
//        System.out.println(Q.front+" "+Q.rear);
        return true;
    }
    private static int  outQueue(MQueue Q){
        if(Q.front==Q.rear){///一开始的状态
            System.out.println("队列已空！");
            System.exit(0);
        }
        return Q.base[Q.front++];
    }
    public static int getElem(MQueue Q){
        if(Q.front==Q.rear){
            System.out.println("队列已空！");
            System.exit(0);
        }
        return Q.base[Q.front];
    }
    public static int getLength(MQueue Q){
        return (Q.rear-Q.front+Maxsize)%Maxsize;///加上一个Maxsize是为了避免出现负数，就像那种，前面出队，后面又入队的情况，rear最后是会在front前面的
    }
    public static void main(String arg[]){
        MQueue MyQ=new MQueue();
        System.out.println("该队列总容量为："+Maxsize);
        int x=-1;
        if(initQueue(MyQ)){
            System.out.println("初始化成功!");
        }
        Scanner scanner =new Scanner(System.in);
        System.out.print("请输入元素的个数：");
        int n=scanner.nextInt();
        int sum=n;
        System.out.print("请依次输入各个元素：");
        while (n-->0){
            x=scanner.nextInt();
            inQueue(MyQ,x);
        }
        System.out.println("队头元素为："+getElem(MyQ));
        System.out.println("队列的长度为："+getLength(MyQ));
        System.out.println("出队元素为：");
        while (sum-->0){
            System.out.print(outQueue(MyQ)+" ");
        }
        System.out.println();
    }
}
